package com.csu.jpetstore_reconfiguration_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csu.jpetstore_reconfiguration_back.common.CommonResponse;
import com.csu.jpetstore_reconfiguration_back.domain.Inventory;
import com.csu.jpetstore_reconfiguration_back.domain.Item;
import com.csu.jpetstore_reconfiguration_back.domain.Product;
import com.csu.jpetstore_reconfiguration_back.domain.User;
import com.csu.jpetstore_reconfiguration_back.mapper.InventoryMapper;
import com.csu.jpetstore_reconfiguration_back.mapper.ProductMapper;
import com.csu.jpetstore_reconfiguration_back.mapper.UserMapper;
import com.csu.jpetstore_reconfiguration_back.service.ItemService;
import com.csu.jpetstore_reconfiguration_back.mapper.ItemMapper;
import com.csu.jpetstore_reconfiguration_back.vo.ItemVO;
import org.apache.tomcat.util.net.openssl.OpenSSLConfCmd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;

/**
* @author 朱嘉辉
* @description 针对表【t_item】的数据库操作Service实现
* @createDate 2024-04-13 20:32:53
*/
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item>
    implements ItemService{
    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    InventoryMapper inventoryMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public CommonResponse<List<ItemVO>> getAllItems() {
        List <Item>itemList=itemMapper.selectList(null);
        List <ItemVO>itemVOList=new ArrayList<>();
        if(itemList.isEmpty())
        {
            return CommonResponse.createForSuccessMessage("没有该Item信息");
        }
        for(Item item: itemList)
        {
            QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("product_id",item.getProductId());
            Product product=productMapper.selectOne(queryWrapper);
            ItemVO itemVO=itemToItemVO(item,product);
            itemVOList.add(itemVO);
        }

        return CommonResponse.createForSuccess(itemVOList);
    }
    //获取单个item
    @Override
    public CommonResponse<ItemVO> getItem(String itemId) {
        Item item=itemMapper.selectById(itemId);
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("product_id",item.getProductId());
        Product product=productMapper.selectOne(queryWrapper);

        ItemVO itemVO=itemToItemVO(item,product);
        return CommonResponse.createForSuccess(itemVO);
    }

    @Override
    public CommonResponse<List<ItemVO>> getItemListByProductId(String productId) {
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("product_id",productId);
        List<Item>itemList=itemMapper.selectList(queryWrapper);
        if(itemList.isEmpty())
        {
            return CommonResponse.createForError("该productId下没有Item");
        }

        Product product=productMapper.selectById(productId);

        List<ItemVO> itemVOList=new ArrayList<>();
        for(Item item: itemList)
        {
            ItemVO itemVO=itemToItemVO(item,product);
            itemVOList.add(itemVO);
        }

        return CommonResponse.createForSuccess(itemVOList);
    }

    //添加item
    @Override
    public CommonResponse<ItemVO> addItem( Item item) {
        ItemVO itemVO=new ItemVO();

        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("item_id",item.getItemId());
        Product product=productMapper.selectById(item.getProductId());

        if(itemMapper.selectList(queryWrapper).isEmpty())
        {
            itemMapper.insert(item);
            itemVO=itemToItemVO(item,product);

            return CommonResponse.createForSuccess("添加成功",itemVO);
        }
        return CommonResponse.createForError("该itemId已存在");
    }
    //修改item信息
    @Override
    public CommonResponse<Item> updateItem(String itemId,Item item) {
        UpdateWrapper<Item> itemUpdateWrappe=new UpdateWrapper<>();
        UpdateWrapper<Product>productUpdateWrapper=new UpdateWrapper<>();
        UpdateWrapper<User>userUpdateWrapper=new UpdateWrapper<>();
        itemUpdateWrappe.like("item_id",itemId);
        itemUpdateWrappe.set("list_price",item.getListPrice())
                .set("unit_cost",item.getUnitCost())
                .set("status",item.getStatus())
                .set("attr1",item.getAttr1())
                .set("attr2",item.getAttr2())
                .set("attr3",item.getAttr3())
                .set("attr4",item.getAttr4())
                .set("attr5",item.getAttr5())
                .set("item_name",item.getItemName());


        itemMapper.update(item,itemUpdateWrappe);
        return CommonResponse.createForSuccess("对"+itemId+"修改成功",item);
    }

    //删除Item
    @Override
    public CommonResponse<Item> deleteItem(String itemId) {
        QueryWrapper<Item>itemQueryWrapper=new QueryWrapper<>();
        QueryWrapper<Inventory>inventoryQueryWrapper=new QueryWrapper<>();

        inventoryQueryWrapper.eq("item_id",itemId);
        inventoryMapper.delete(inventoryQueryWrapper);
        itemQueryWrapper.eq("item_id",itemId);
        Item item=itemMapper.selectById(itemId);
        itemMapper.delete(itemQueryWrapper);
        return CommonResponse.createForSuccess("删除"+itemId+"成功",item);
    }

    private ItemVO itemToItemVO(Item item, Product product)
    {
        ItemVO itemVO=new ItemVO();
        itemVO.setAttr1(item.getAttr1());
        itemVO.setAttr2(item.getAttr2());
        itemVO.setAttr3(item.getAttr3());
        itemVO.setAttr4(item.getAttr4());
        itemVO.setAttr5(item.getAttr5());
        itemVO.setProductId(item.getProductId());
        itemVO.setItemId(item.getItemId());
        itemVO.setListPrice(item.getListPrice());
        itemVO.setUnitCost(item.getUnitCost());
        itemVO.setSupplier(item.getSupplier());
        itemVO.setStatus(item.getStatus());
        itemVO.setItemName(item.getItemName());

        itemVO.setDescn(product.getDescn());
        itemVO.setImgUrl(product.getImgUrl());
        itemVO.setCategory(product.getCategory());

        Inventory inventory=inventoryMapper.selectById(item.getItemId());
        if(inventory==null)
        {
            inventory=new Inventory();
            inventory.setItemId(item.getItemId());
            inventory.setQty(1);
            inventoryMapper.insert(inventory);
        }
        itemVO.setQty(inventory.getQty());
        return itemVO;
    }
}




